package com.xiaoq.system.endpoint;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xiaoq.coms.GenericListResponse;
import com.xiaoq.coms.GenericResponse;
import com.xiaoq.coms.util.NetUtil;
import com.xiaoq.sites.entity.SitesHotSearchEntity;
import com.xiaoq.sites.service.ISitesService;
import com.xiaoq.system.entity.ZoneMasterEntity;
import com.xiaoq.system.service.IZoneMasterService;
import com.xiaoq.system.vo.ZoneMasterDetailVo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api/v1/zonemaster")
public class ZoneMasterEndpoint {

	// @Reference(interfaceClass=IZoneMasterApi.class, check=false, version="1.0.0")
	@Autowired
	private IZoneMasterService zoneMasterService;

	@Autowired
	private ISitesService sitesService;

	private static final class ZoneMasterListResponse extends GenericListResponse<ZoneMasterDetailVo> {
	};

	@ApiOperation(value = "（爬虫专用 code 如果有多个就选levels为1的项就是对应的名称的城市编码)城市名字查询城市列表", notes = "（爬虫专用)城市名字查询城市列表")
	@ApiResponses({ @ApiResponse(code = 200, response = ZoneMasterListResponse.class, message = "（爬虫专用)城市名字查询城市列表") })
	@GetMapping(value = "/reptileZone", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> catchCityName(@RequestParam String cityName) {
		List<ZoneMasterEntity> zoneMasterList = zoneMasterService.findAllZoneMasterByCityName(cityName);
		List<ZoneMasterDetailVo> zoneMasterVoList = new ArrayList<>();
		zoneMasterEntity2zoneMasterVo(zoneMasterList, zoneMasterVoList);
		return ZoneMasterListResponse.listNoCount(zoneMasterVoList);
	}



	@ApiOperation(value = "行政区划列表", notes = "查询所有的行政区划信息")
	@ApiResponses({ @ApiResponse(code = 200, response = ZoneMasterListResponse.class, message = "行政区划列表") })
	@GetMapping(value = "/", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> zoneMasterAll() {
		List<ZoneMasterEntity> zoneMasterList = zoneMasterService.findAllZoneMaster();
		List<ZoneMasterDetailVo> zoneMasterVoList = new ArrayList<>();
		zoneMasterEntity2zoneMasterVo(zoneMasterList, zoneMasterVoList);
		return ZoneMasterListResponse.listNoCount(zoneMasterVoList);
	}

	@ApiOperation(value = "热门活动城市列表", notes = "热门活动城市列表")
	@ApiResponses({ @ApiResponse(code = 200, response = ZoneMasterListResponse.class, message = "热门活动城市列表") })
	@GetMapping(value = "/hot_activities", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> zoneMaste4HostActivities() {
		List<ZoneMasterEntity> zoneMasterList = zoneMasterService.find4HotActivities();
		List<ZoneMasterDetailVo> zoneMasterVoList = new ArrayList<>();
		zoneMasterEntity2zoneMasterVo(zoneMasterList, zoneMasterVoList);
		return ZoneMasterListResponse.listNoCount(zoneMasterVoList);
	}

	@ApiOperation(value = "首页切换城市列表", notes = "首页切换城市列表")
	@ApiResponses({ @ApiResponse(code = 200, response = ZoneMasterListResponse.class, message = "首页切换城市列表") })
	@GetMapping(value = "/cities", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> zoneMaste4ExchangeCity() {
		List<ZoneMasterEntity> zoneMasterList = zoneMasterService.findAllZoneMaster(null,null);
		List<ZoneMasterDetailVo> zoneMasterVoList = new ArrayList<>();
		zoneMasterEntity2zoneMasterVo(zoneMasterList, zoneMasterVoList);
		return ZoneMasterListResponse.listNoCount(zoneMasterVoList);
	}

	@ApiOperation(value = "位置营销热门城市", notes = "位置营销热门城市")
	@ApiResponses({ @ApiResponse(code = 200, response = ZoneMasterListResponse.class, message = "位置营销热门城市") })
	@GetMapping(value = "/hotcity4position", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> hotCity4PositionMarket() {
		List<SitesHotSearchEntity> hotSearchList =  sitesService.getSitesHotSearchList();
		List<ZoneMasterDetailVo> zoneMasterVoList = new ArrayList<>();

		if(CollectionUtils.isNotEmpty(hotSearchList)){
			List<String> zoneCityCodeList = new ArrayList<>();
			for(SitesHotSearchEntity sitesHotSearch : hotSearchList){
				zoneCityCodeList.add(sitesHotSearch.getZoneCity());
			}

			List<ZoneMasterEntity> zoneMasterList = zoneMasterService.getZoneMasterListByCityCodeList(zoneCityCodeList);
			zoneMasterEntity2zoneMasterVo(zoneMasterList, zoneMasterVoList);
		}
		return ZoneMasterListResponse.listNoCount(zoneMasterVoList);
	}


	private void zoneMasterEntity2zoneMasterVo(List<ZoneMasterEntity> zoneMasterList, List<ZoneMasterDetailVo> zoneMasterVoList) {
		ZoneMasterDetailVo zoneMasterDetailVo;
		if (CollectionUtils.isNotEmpty(zoneMasterList)) {
			for (ZoneMasterEntity zoneMaster : zoneMasterList) {
				zoneMasterDetailVo = new ZoneMasterDetailVo();
				BeanUtils.copyProperties(zoneMaster, zoneMasterDetailVo);
				zoneMasterVoList.add(zoneMasterDetailVo);
			}
		}
	}

	@ApiOperation(value = "更新市级的经纬度", notes = "更新市级的经纬度")
	@ApiResponses({ @ApiResponse(code = 200, response = ZoneMasterListResponse.class, message = "更新市级的经纬度") })
	@GetMapping(value = "/updatelongitude", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> updateLotitude() {
		List<ZoneMasterEntity> zoneMasterList = zoneMasterService.findAllZoneMaster(null,1);

		int success = 0, error = 0;

		if(CollectionUtils.isNotEmpty(zoneMasterList)){
			for (ZoneMasterEntity zoneMasterEntity : zoneMasterList){
				String name = zoneMasterEntity.getName();
				try {
					String result = NetUtil.readContentFromGet("http://api.map.baidu.com/geocoder/v2/?address="+name+"&output=json&ak=vDxkYvv57oYnENm2tg8FVGCfZv3CSwgY&callback=showLocation");
					result = StringUtils.substringBetween(result, "(", ")");
					JSONObject jsonObject = JSON.parseObject(result);
					if(jsonObject != null && jsonObject.getIntValue("status")==0){
						JSONObject lngLatObject = jsonObject.getJSONObject("result").getJSONObject("location");
						double lng = lngLatObject.getDoubleValue("lng");
						double lat = lngLatObject.getDoubleValue("lat");
						ZoneMasterEntity update = new ZoneMasterEntity();
						update.setId(zoneMasterEntity.getId());
						update.setLongitude(lng);
						update.setLatitude(lat);
						zoneMasterService.updateById(update);
						success ++;
					}else {
						error ++;
						System.out.println("error************" + name + "**************: " + zoneMasterEntity.getId());
					}
				}catch (Exception e){
					e.printStackTrace();
					error ++;
				}
			}
		}

		System.out.println("success: " + success);
		System.out.println("error: " + error);

		return GenericResponse.ok();
	}

}
